/*
 * light_sol.c
 *
 * Created: 10/29/2013 2:40:16 PM
 *  Author: Tim
 */ 

//FYI another "../" is in the solution file include call because these files are contained a directory one step deeper than the standard files
#include "../../src/Epiphany.h"

//function  protos.  You don't need functions to solve this module.  I just created them for simplicity.
void printConversion();

void printVoltage();

void setup()
{
	//put all your initializations here
	
	usbInit(115200);	//the baud does not matter you just need to choose one
	adcInit();			//can't read from the ADC without this
	RTC_DelayInit();		//can't create RTC delays without calling this first
	
	sei();				//the serial port code is interrupt driven.  We have to have the 
}

void loop()
{
//	run this function for part I
	printConversion();
	
//	run this function for part II
//	printVoltage();

}


void printConversion()
{
	if(delayOver)
	{
		RTC_Delay_ms(100); //10Hz means we need to print every 100 mS => 0.1 S
		fprintf(&USB_str,"ADC CH%d = %d\r\n",0,analogRead(0));	//FYI X-CTU only requires \r to create a new line and carriage return. using \r\n is more proper though.  
	}
}
	
void printVoltage()
{
	if(delayOver)
	{
		RTC_Delay_ms(100); //10Hz means we need to print every 100 mS => 0.1 S
		
		//By default the ADC is setup to read signed voltages ranging from +-5V
		//The resolution of the ADC has been set to 14-bit
		//since one bit will be used to determine the sign (positive of negative) the resolution of 0-5V is 0-8191 (comes from 2^13 - 1)
		
		fprintf(&USB_str,"ADC CH%d = % 1.3f\r\n",0, (analogRead(0)*5/8191.0) );	//FYI X-CTU only requires \r to create a new line and carriage return. using \r\n is more propper though.
	}
}